<?php
/**
 * 配置表
 * @author qianxun
 *
 */
class configModel extends MY_Model{
    function configModel(){
        parent::MY_Model();
        $this->table = 'Config';
        $this->load->library('Cache');
    }
    /**
     * 包装数据(non-PHPdoc)
     * @see MY_Model::packing()
     */
    function packing($list = array()){
        foreach ($list as $k => $v){
            $v['value'] = $v['value'] ? unserialize($v['value']) : NULL;
            $list[$k] = $v;
        }
        return $list;
    }
    /**
     * 保存配置数据
     * @param unknown $key
     * @param unknown $data
     */
    function saveConfigData($key, $data){
        $data = array(
            'value' => serialize($data),
        );
        $findData = $this->getData(array('key'=>$key));
        if ($findData){
            $this->edit($findData['id'], $data);
        }else{
            $data['key'] = $key;
            $this->add($data);
        }
        $this->createCache();
        return true;
    }
    /**
     * 获取配置数据
     * @param array $key
     * @return NULL | mixed
     */
    function getConfigData($key){
        $this->cache->expire = 86400;//设置过期时间为一天
        $configList = $this->cache->get('configList');
        if (!$configList){
            //创建缓存
            $this->createCache();
            $configList = $this->cache->get('configList');
        }
        return isset($configList[$key]) ? $configList[$key] : NULL;
    }
    /**
     * 创建缓存
     */
    function createCache(){
        $configList = array();
        $findList = $this->getNewList(array('id > '=>0));
        foreach ($findList as $v){
            $configList[$v['key']] = $v['value'];
        }
        $this->cache->set('configList', $configList);//缓存所有配置列表
    }
}